package cn.ctyun.ipush.dao;

import cn.ctyun.ipush.model.DataSourceModel;
import cn.ctyun.ipush.model.EmailModel;
import cn.ctyun.ipush.utils.DateUtils;
import cn.ctyun.ipush.vo.EmailVo;
import com.dexcoder.dal.JdbcDao;
import com.dexcoder.dal.build.Criteria;
import com.dexcoder.dal.spring.page.PageControl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: user
 * Date: 16-7-4
 * Time: 上午10:41
 * To change this template use File | Settings | File Templates.
 */
@Component
public class EmailDao {
    @Autowired
    private JdbcDao jdbcDao;

    @Autowired
    private PageControl pageControl;

    /**
     * 保存
     *
     * @param emailModel
     */
    public void save(EmailModel emailModel) {
        jdbcDao.save(emailModel);
    }
    /**
     * 更新 ,主键Id 必须存在;此数据是修改后的完整对象
     *
     * @param emailModel
     */
    public int update(EmailModel emailModel) {
        if (StringUtils.isEmpty(emailModel.getEmailModelId()))
            throw new RuntimeException("emailModel 主键id 必须设置");
        emailModel.setModifyDate(DateUtils.dateToString(new Date()));
        return jdbcDao.update(emailModel);
    }


    /**
     * 更新 主键Id 必须存在
     *
     * @param emailModelId
     */
    public int delete(String emailModelId) {
        if (StringUtils.isEmpty(emailModelId))
            throw new RuntimeException("emailModelId 主键id 必须设置");
        Criteria criteria = Criteria.update(EmailModel.class).where("emailModelId", new Object[]{emailModelId}).set("isDeleted", "1");
        return jdbcDao.update(criteria);
    }

    /**
     * 查询单个,根据ID
     *
     * @param emailModelId
     * @return
     */
    public EmailModel querySingleResult(String emailModelId) {
        if (StringUtils.isEmpty(emailModelId))
            throw new RuntimeException("emailModelId 主键id 必须设置");
        Criteria criteria = Criteria.select(EmailModel.class).where("emailModelId", new Object[]{emailModelId}).where("and isDeleted", new Object[]{"0"});
        return jdbcDao.querySingleResult(criteria);
    }

    /**
     * 查询单个，根据名称
     *
     * @param emailName
     * @return
     */
    public EmailModel querySingleResultByName(String emailName) {
        if (StringUtils.isEmpty(emailName))
            throw new RuntimeException("emailName 为空");
        Criteria criteria = Criteria.select(EmailModel.class).where("emailName", new Object[]{emailName}).where("and isDeleted", new Object[]{"0"});
        return jdbcDao.querySingleResult(criteria);
    }

    /**
     * 模糊查询多个
     *
     * @param criteria
     * @return
     */
    public List<EmailModel> queryList(Criteria criteria) {
        return jdbcDao.queryList(criteria);
    }

    /**
     * 模糊查询多个
     *
     * @param emailModel
     * @return
     */
    public List<EmailModel> queryList(EmailModel emailModel) {
        return jdbcDao.queryList(emailModel);
    }

    /**
     * 查询全部
     *
     * @param emailModel
     * @return
     */
    public List<EmailModel> queryAll(EmailModel emailModel) {
        return jdbcDao.queryList(emailModel);
    }

    /**
     * 分页按条件查询
     * @param currentPage
     * @param itemPages
     * @param emailVo
     * @return
     */
    public List<EmailModel> queryListByPage(int currentPage, int itemPages, EmailVo emailVo) {
        pageControl.performPage(currentPage, itemPages);
        Criteria criteria = Criteria.select(EmailModel.class).where("createUserid", new Object[]{emailVo.getCreateUserid()});
        jdbcDao.queryList(criteria);
        return pageControl.getPager().getList(EmailModel.class);
    }

}
